// 实现这个项目的构建任务
const {
  src,
  dest,
  series,
  parallel,
  watch
} = require('gulp');
const bs = require('browser-sync').create();
const plugins = require('gulp-load-plugins')();
const del = require('del');

const data = {
  menus: [{
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [{
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}

// 清除临时目录
const clean = () => {
  return del(['template', 'dist'])
}

// 查找.scss文件流、转化流、输出流
// 这个位置不用压缩？ 这个css任务是开发时用得。build得时候通过useref压缩代码。js、html同理
const css = () => {
  return src('src/assets/styles/**.scss', {
      base: 'src'
    })
    .pipe(plugins.sass())
    .pipe(dest('template'))
    .pipe(bs.reload({
      stream: true
    }))
}


// 查找.js文件流、转化流、输出流
const js = () => {
  return src('src/assets/scripts/**.js', {
      base: 'src'
    })
    .pipe(plugins.babel({
      presets: ['@babel/preset-env']
    }))
    .pipe(dest('template'))
    .pipe(bs.reload({
      stream: true
    }))
}

// 查找.html文件流、转化流、输出流
const html = () => {
  return src('src/**/*.html', {
      base: 'src'
    })
    .pipe(plugins.swig({
      data,
      defaults: {
        cache: false
      }
    }))
    .pipe(dest('template'))
    .pipe(bs.reload({
      stream: true
    }))
}

const image = () => {
  return src('src/assets/images/**', {
      base: 'src'
    })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}

const font = () => {
  return src('src/assets/fonts/**', {
      base: 'src'
    })
    .pipe(plugins.imagemin())
    .pipe(dest('dist'))
}


const extra = () => {
  return src('public/**', {
    base: 'public'
  }).pipe(dest('dist'))
}

const useref = () => {
  return src('template/*.html', {
      base: 'template'
    })
    .pipe(plugins.useref({
      searchPath: ['template', '.']
    }))
    .pipe(plugins.if(/\.js$/, plugins.uglify()))
    .pipe(plugins.if(/\.css$/, plugins.cleanCss()))
    .pipe(plugins.if(/\.html$/, plugins.htmlmin({
      removeComments: true, //清除HTML注释
      collapseWhitespace: true, //压缩HTML
      // collapseBooleanAttributes: true, //省略布尔属性的值 <input checked="true"/> ==> <input />
      // removeEmptyAttributes: true, //删除所有空格作属性值 <input id="" /> ==> <input />
      // removeScriptTypeAttributes: true, //删除<script>的type="text/javascript"
      // removeStyleLinkTypeAttributes: true, //删除<style>和<link>的type="text/css"
      minifyJS: true, //压缩页面JS
      minifyCSS: true //压缩页面CSS
    })))
    .pipe(dest('dist'))
}

const server = () => {
  watch('src/assets/styles/**.scss', css);
  watch('src/assets/scripts/**.js', js);
  watch('src/**/*.html', html);
  watch(['src/assets/images/**', 'src/assets/fonts/**', 'public/**']).on("change", bs.reload);
  bs.init({
    server: {
      baseDir: ['template', 'src', 'public'],
      routes: {
        "/node_modules": "node_modules"
      },
    },
    open: false,
    port: 8080,
    notify: false
  })
}


const compile = parallel(css, js, html)
const dev = series(compile, server)
const build = series(clean, parallel(
  series(compile, useref),
  image,
  font,
  extra
))

module.exports = {
  clean,
  dev,
  build
}
